package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ruoyi.business.domain.FollowUpRecord;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.PotentialCustomerMapper;
import com.ruoyi.business.domain.PotentialCustomer;
import com.ruoyi.business.service.IPotentialCustomerService;
import com.ruoyi.common.core.text.Convert;

/**
 * 潜在客户Service业务层处理
 * 
 * @author wolfcode
 * @date 2021-07-26
 */
@Service
public class PotentialCustomerServiceImpl implements IPotentialCustomerService 
{
    @Autowired
    private PotentialCustomerMapper potentialCustomerMapper;

    /**
     * 查询潜在客户
     * 
     * @param id 潜在客户ID
     * @return 潜在客户
     */
    @Override
    public PotentialCustomer selectPotentialCustomerById(Long id)
    {
        return potentialCustomerMapper.selectPotentialCustomerById(id);
    }

    /**
     * 查询潜在客户列表
     * 
     * @param potentialCustomer 潜在客户
     * @return 潜在客户
     */
    @Override
    public List<PotentialCustomer> selectPotentialCustomerList(PotentialCustomer potentialCustomer)
    {
        if(SecurityUtils.getSubject().hasRole("admin")){
            List<PotentialCustomer> potentialCustomerListByAdmin=potentialCustomerMapper.selectPotentialCustomerList(potentialCustomer);
            return potentialCustomerListByAdmin;
        }
        if(SecurityUtils.getSubject().hasRole("shopOwner")){
            List<PotentialCustomer> potentialCustomerListByAdmin=potentialCustomerMapper.selectPotentialCustomerList(potentialCustomer);
            return potentialCustomerListByAdmin;
        }
        Long userId = ShiroUtils.getUserId();
        List<PotentialCustomer> potentialCustomerList = potentialCustomerMapper.selectPotentialCustomerList(potentialCustomer);
        List<PotentialCustomer> potentialCustomerByUserId=new ArrayList<>();
        for (PotentialCustomer customer : potentialCustomerList) {
            if(customer.getFollowUpUser()==userId){
                potentialCustomerByUserId.add(customer);
            }
        }
        return potentialCustomerByUserId;
    }

    /**
     * 新增潜在客户
     * 
     * @param potentialCustomer 潜在客户
     * @return 结果
     */
    @Override
    public int insertPotentialCustomer(PotentialCustomer potentialCustomer)
    {
    PotentialCustomer potentialCustomerByPhone = potentialCustomerMapper.selectPotentialCustomerByPhone(potentialCustomer.getPhone());
    if(potentialCustomerByPhone!=null){
        return 0;
    }
    potentialCustomer.setCreateTime(DateUtils.getNowDate());
    potentialCustomer.setFollowUpUser(ShiroUtils.getUserId());
    return potentialCustomerMapper.insertPotentialCustomer(potentialCustomer);
}

    /**
     * 修改潜在客户
     * 
     * @param potentialCustomer 潜在客户
     * @return 结果
     */
    @Override
    public int updatePotentialCustomer(PotentialCustomer potentialCustomer)
    {
        return potentialCustomerMapper.updatePotentialCustomer(potentialCustomer);
    }

    /**
     * 删除潜在客户对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePotentialCustomerByIds(String ids)
    {
        return potentialCustomerMapper.deletePotentialCustomerByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除潜在客户信息
     * 
     * @param id 潜在客户ID
     * @return 结果
     */
    @Override
    public int deletePotentialCustomerById(Long id)
    {
        return potentialCustomerMapper.deletePotentialCustomerById(id);
    }

    @Override
    public int insertFollowUpRecord(FollowUpRecord followUpRecord) {
        int count = potentialCustomerMapper.insertFollowUpRecord(followUpRecord);
        potentialCustomerMapper.updatePotentialCustomerByfollowUpTime(followUpRecord);
        return count;
    }

    @Override
    public List<PotentialCustomer> selectPotentialCustomerHistoryList(Long potentialCustomerId) {
        PotentialCustomer potentialCustomer = new PotentialCustomer();
        potentialCustomer.setFollowUpUser(ShiroUtils.getUserId());
        potentialCustomer.setId(potentialCustomerId);
        List<PotentialCustomer> potentialCustomerList=potentialCustomerMapper.selectPotentialCustomerHistoryList(potentialCustomer);
        return potentialCustomerList;
    }

    @Override
    public int deleteFollowUpRecordByIds(String id) {
        return potentialCustomerMapper.deleteFollowUpRecordByIds(id);
    }
}
